package com.yzt.zhuju.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


import com.yzt.zhuju.RXingUtils;
import com.yzt.p_base.AppUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能：搜索历史的数据库
 */
public class VoicerHistorySearchHelper extends SQLiteOpenHelper {
    /**
     * 数据库名
     */
    private static final String DB_NAME = "_history_search_1.db";
    private static final int DB_VERSION = 2;

    private static SQLiteDatabase sqliteDB;
    private static VoicerHistorySearchHelper voicerHistorySearchHelper;


    public static VoicerHistorySearchHelper getInstance() {
        if (voicerHistorySearchHelper == null) {
            voicerHistorySearchHelper = new VoicerHistorySearchHelper(AppUtils.getApp());
        }
        return voicerHistorySearchHelper;
    }


    String DB_TABLE_VOICERHISTORY_NAME = "voicer_history_search";

    //字段
    String DB_KEY_ID = "_id";//id
    String DB_KEY_NAME = "name";//天


    String CREATE_TBL_MOODDIARY_INFO = " CREATE TABLE IF NOT EXISTS "//
            + DB_TABLE_VOICERHISTORY_NAME + "(" + DB_KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " //
            + DB_KEY_NAME + " TEXT ) ";


    public VoicerHistorySearchHelper(Context context) {
        super(context, RXingUtils.getUserId() + DB_NAME, null, DB_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TBL_MOODDIARY_INFO);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(" DROP TABLE IF EXISTS " + DB_TABLE_VOICERHISTORY_NAME);
    }

    /**
     *
     */
    public void insertVoicerHistory(String key) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.beginTransaction();//开启事务
        Cursor cursor = null;
        try {
            cursor = db.query(DB_TABLE_VOICERHISTORY_NAME, null, DB_KEY_NAME + "=? ", new String[]{key}, null, null, null);
            if (cursor.moveToFirst()) { // 存在
                ContentValues contentValues = ContentValues(key);
                db.update(DB_TABLE_VOICERHISTORY_NAME, contentValues, DB_KEY_NAME + "=?", new String[]{key});
            } else {
                ContentValues contentValues = ContentValues(key);
                db.insert(DB_TABLE_VOICERHISTORY_NAME, null, contentValues);
            }
            db.setTransactionSuccessful();//设置事务的标志为True
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.endTransaction();//结束事务,有两种情况：commit,rollback,
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }
    }

    /**
     * @return
     */
    public List<String> getVoicerHistoryList() {
        List<String> list = new ArrayList<>();
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = null;
        db.beginTransaction();//开启事务
        try {
            cursor = db.query(DB_TABLE_VOICERHISTORY_NAME, null, null, null, null, null, null);
            while (cursor.moveToNext()) {
                list.add(cursor.getString(cursor.getColumnIndex(DB_KEY_NAME)));
            }
            db.setTransactionSuccessful();//设置事务的标志为True
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.endTransaction();//结束事务,有两种情况：commit,rollback,
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }
        return list;
    }


    /**
     *
     */
    public void deleteVoicerHistory(String key) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.beginTransaction();//开启事务
//        Cursor cursor = null;
        try {
            db.delete(DB_TABLE_VOICERHISTORY_NAME, DB_KEY_NAME + "=? ", new String[]{key});
            db.setTransactionSuccessful();//设置事务的标志为True
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.endTransaction();//结束事务,有两种情况：commit,rollback,
//            if (cursor != null && !cursor.isClosed()) {
//                cursor.img_close();
//            }
        }
    }


    /**
     *
     */
    public void deleteTable() {
        SQLiteDatabase db = this.getWritableDatabase();
        db.beginTransaction();//开启事务
        try {
            db.execSQL("DELETE FROM " + DB_TABLE_VOICERHISTORY_NAME);
            db.setTransactionSuccessful();
        }catch (Exception e) {

        }finally{
            //结束事务
            db.endTransaction();
        }
    }


    public ContentValues ContentValues(String key) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(DB_KEY_NAME, key);
        return contentValues;
    }
}
